﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Libs.Db;

namespace Libs.Partner
{
    public class Partner
    {
        public int PartnerID { get; set; }
        public string FullName { get; set; }
        public string FullNameEn { get; set; }
        public int CompanyID { get; set; }
        public string Position { get; set; }
        public DateTime Birthday { get; set; }
        public string Email { get; set; }
        public string Fax { get; set; }
        public string Mobile { get; set; }
        public string WorkPhone { get; set; }
        public string HomePhone { get; set; }
        public string HomeAddress { get; set; }
        public string WorkAddress { get; set; }
        public string Website { get; set; }
        public DateTime LogTime { get; set; }
        public string Description { get; set; }
        public int ReturnValue { get; set; }

        public Partner()
        {

        }

        ~Partner()
        {

        }

        public virtual void Dispose()
        {

        }

        /// <summary>
        /// Lấy giá trị Partner theo ID
        /// </summary>
        /// <param name="PartnerID"></param>
        /// <returns></returns>
        public Partner Get(int partnerID)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetInstanceSP<Partner>("sp_Partner_Select",
                new SqlParameter("@PartnerID", partnerID));
        }

        /// <summary>
        /// Lấy giá trị Partner theo ID
        /// </summary>
        /// <returns></returns>
        public Partner Get()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetInstanceSP<Partner>("sp_Partner_Select",
                new SqlParameter("@PartnerID", PartnerID));
        }

        /// <summary>
        /// Thêm mới Partner
        /// </summary>
        public void Add()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            SqlParameter[] pars = new SqlParameter[15];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@FullName", FullName);
            pars[2] = new SqlParameter("@FullNameEn", FullNameEn);
            pars[3] = new SqlParameter("@CompanyID", CompanyID);
            pars[4] = new SqlParameter("@Position", Position);
            pars[5] = Birthday == DateTime.MinValue ? new SqlParameter("@Birthday", DBNull.Value) : new SqlParameter("@Birthday", Birthday);
            pars[6] = new SqlParameter("@Email", Email);
            pars[7] = new SqlParameter("@Fax", Fax);
            pars[8] = new SqlParameter("@Mobile", Mobile);
            pars[9] = new SqlParameter("@WorkPhone", WorkPhone);
            pars[10] = new SqlParameter("@HomePhone", HomePhone);
            pars[11] = new SqlParameter("@HomeAddress", HomeAddress);
            pars[12] = new SqlParameter("@WorkAddress", WorkAddress);
            pars[13] = new SqlParameter("@Website", Website);
            pars[14] = new SqlParameter("@Description", Description);
    
            db.ExecuteNonQuerySP("sp_Partner_Insert", pars);
            PartnerID = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Cập nhật Partner
        /// </summary>
        public void Update()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            SqlParameter[] pars = new SqlParameter[16];
            pars[0] = new SqlParameter("@ReturnValue", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = new SqlParameter("@PartnerID", PartnerID);
            pars[2] = new SqlParameter("@FullName", FullName);
            pars[3] = new SqlParameter("@FullNameEn", FullNameEn);
            pars[4] = new SqlParameter("@CompanyID", CompanyID);
            pars[5] = new SqlParameter("@Position", Position);
            pars[6] = Birthday == DateTime.MinValue ? new SqlParameter("@Birthday", DBNull.Value) : new SqlParameter("@Birthday", Birthday);
            pars[7] = new SqlParameter("@Email", Email);
            pars[8] = new SqlParameter("@Fax", Fax);
            pars[9] = new SqlParameter("@Mobile", Mobile);
            pars[10] = new SqlParameter("@WorkPhone", WorkPhone);
            pars[11] = new SqlParameter("@HomePhone", HomePhone);
            pars[12] = new SqlParameter("@HomeAddress", HomeAddress);
            pars[13] = new SqlParameter("@WorkAddress", WorkAddress);
            pars[14] = new SqlParameter("@Website", Website);
            pars[15] = new SqlParameter("@Description", Description);

            db.ExecuteNonQuerySP("sp_Partner_Update", pars);
            ReturnValue = Convert.ToInt32(pars[0].Value);
        }

        /// <summary>
        /// Xoá Partner
        /// </summary>
        /// <param name="PartnerID"></param>
        public void Delete(int partnerID)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);

            db.ExecuteNonQuerySP("sp_Partner_Delete",
                new SqlParameter("@PartnerID", partnerID));
        }

        /// <summary>
        /// Xoá Partner
        /// </summary>
        public void Delete()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);

            db.ExecuteNonQuerySP("sp_Partner_Delete",
                new SqlParameter("@PartnerID", PartnerID));
        }

        public List<Partner> GetList()
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            return db.GetListSP<Partner>("sp_Partner_SelectList");
        }


        public DataTable Search(int companyID, string keyword, int pageIndex, int pageSize, ref int totalRecord)
        {
            DbHelper db = new DbHelper(Config.PartnerConnectionStrings);
            SqlParameter[] pars = new SqlParameter[5];
            pars[0] = new SqlParameter("@TotalRecord", SqlDbType.Int) { Direction = ParameterDirection.Output };
            pars[1] = companyID == 0 ? new SqlParameter("@CompanyID", DBNull.Value) : new SqlParameter("@CompanyID", companyID);
            pars[2] = new SqlParameter("@Keyword", keyword);
            pars[3] = new SqlParameter("@PageIndex", pageIndex);
            pars[4] = new SqlParameter("@PageSize", pageSize);

            DataTable dt = db.GetDataTableSP("sp_Partner_SearchPage", pars);
            totalRecord = Convert.ToInt32(pars[0].Value);
            return dt;
        }

        /// <summary>
        /// Chuyển chữ tiếng Việt sang không dấu
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static String UCS2Lower(String s)
        {
            string[] aUTF8Lower = { "a", "á", "à", "ả", "ã", "ạ", "ă", "ắ", "ằ", "ẳ", "ẵ", "ặ", "â", "ấ", "ầ", "ẩ", "ẫ", "ậ", "đ", "e", "é", "è", "ẻ", "ẽ", "ẹ", "ê", "ế", "ề", "ể", "ễ", "ệ", "i", "í", "ì", "ỉ", "ĩ", "ị", "o", "ó", "ò", "ỏ", "õ", "ọ", "ô", "ố", "ồ", "ổ", "ỗ", "ộ", "ơ", "ớ", "ờ", "ở", "ỡ", "ợ", "u", "ú", "ù", "ủ", "ũ", "ụ", "ư", "ứ", "ừ", "ử", "ữ", "ự", "y", "ý", "ỳ", "ỷ", "ỹ", "ỵ" };
            String[] aUCS2Lower = { "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "d", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "e", "i", "i", "i", "i", "i", "i", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "u", "y", "y", "y", "y", "y", "y" };

            // Chuyển sang chữ không dấu
            int n = aUTF8Lower.Length;
            for (int i = 0; i < n; i++)
            {
                s = s.Replace(aUTF8Lower[i], aUCS2Lower[i]);
            }

            while (s.IndexOf("  ") >= 0)
            {
                s = s.Replace("  ", " ");
            }

            return s;
        }

    }
}
